var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFold: true,
    conpany:'公司名称!',
    showModalDataImg:'',
    changLogo:false,
    showData:false
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
      var that = this;
      wx.showLoading({
        title: '加载中!',
      });
      wx.request({
        url: app.globalData.phpUrl + 'user/api',
        method: 'POST',
        data: {
          api_name: 'company_detail',
          openid: app.globalData.userData.openOId,
        },
        success: function (e) {
          wx.hideLoading();
          if (e.data.code == 1) {
            wx.getImageInfo({
              src: e.data.data.logo,
              success: function (res) {
                that.setData({
                  logoW: res.width,
                  logoH: res.height,
                  bl: (res.width / res.height)*150 + 150
                })
              }
            });

            that.setData({
              showData: true,
              conpany: e.data.data.company_name,
              logo: e.data.data.logo,
              status: e.data.data.status,
            });
            
            wx.hideLoading()
          } else {
            wx.hideLoading()
            that.setData({
              showData:true,
              status: 3
            });
          };
        }
      })
  },

  edit:function(){
     wx.navigateTo({
       url: 'attestation/attestation',
     })
  },
  changLogo:function(){
    var that = this;
    if (that.data.status != 1) {
      wx.showToast({
        title: '企业未通过审核，可通过编辑企业信息重新提交企业信息',
        icon: 'none'
      });
    }else{
      wx.chooseImage({
        success: function (res) {
          that.setData({
            changLogo: true,
            showModalDataImg: res.tempFilePaths[0]
          })
        },
        fail: function () {
          that.setData({
            changLogo: false,
            showModalDataImg: '',
          })
        }
      })
    }
  },

  cancel: function () {
    var that = this;
    that.setData({
      changLogo: false,
      showModalDataImg: ''
    })
  },

  change:function(){
    var that = this;   
    wx.showLoading({
      icon:'none',
      title: '上传中...'
    });
    that.setData({
      changLogo: false,
    })
    wx.uploadFile({
      url: app.globalData.phpUrl + 'Publish/api',
      filePath: that.data.showModalDataImg,
      name: 'img',
      header: {
        "Content-Type": "multipart/form-data",
      },
      formData: {
        'api_name': 'upload',
        'type': 1,
      },
      success: function (res) {
        wx.request({
          url: app.globalData.phpUrl + 'user/api',
          method: 'POST',
          data: {
            api_name: 'company_ident',
            openid: app.globalData.userData.openOId,
            company_name: that.data.conpany,
            logo: JSON.parse(res.data).data,
          },
          success: function (e) {
            wx.hideLoading();
            if (e.data.code == 1) {
              that.setData({
                showModalDataImg: '',
                logo: JSON.parse(res.data).data
              });
              wx.showToast({
                title: '更换成功!',
                duration: 2000
              })
            } else {
              wx.showToast({
                image: "../../../images/attend.png",
                title: '更换失败!',
                duration: 2000
              })
            }
          }
        })
      },
      fail:function(res){
        wx.showToast({
          title: '上传失败!',
          icon: 'none',
          duration: 2000,
        });
        that.setData({
          changLogo: false,
          showModalDataImg: ''
        })
      }
    });
  },
})